﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace TS_XY_Train_Repaire.UserControll.Locomotive.TrainRepaire.Work
{
    public partial class ycl_Locomotive_TrainRepaire_Work_AllDataList : UserControl
    {
        public string _LoadBMWID = "-1";
        public ycl_Locomotive_TrainRepaire_Work_AllDataList()
        {
            InitializeComponent();
        }
        public void _LoadData()
        {
            tl_Main.Nodes.Clear();
            ArrayList pList = new ArrayList();
            TR_BU_Work_DataComplete_List p = new TR_BU_Work_DataComplete_List();
            string strSQL = "select f_id,CONVERT(varchar(100), f_pstime, 23) as f_pstime,CONVERT(varchar(100), f_petime, 23) as f_petime,f_ttname+f_name as f_name,(case f_state when 9 then CONVERT(varchar(100), f_etime, 120) else '' end) as f_etime,(select f_name from TS_S_T_P_Train_RP where f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_ttid,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state=9) and f_bmwid=TS_TR_BU_Work_Main.f_id)*100/(select case count(*) when 0 then 1 else count(*) end  from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id) as f_percent,(select f_name from TS_S_T_S_RP_DataState where f_del=0 and f_id=TS_TR_BU_Work_Main.f_state) as f_statename,(case f_state when 9 then datediff(day,f_petime,f_etime) else  datediff(day,f_petime,getdate()) end) as f_outtime from TS_TR_BU_Work_Main where f_id=" + _LoadBMWID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TR_BU_Work_DataComplete_List();
                p.f_name = read["f_name"].ToString();
                p.f_id = "M" + read["f_id"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_etime = read["f_etime"].ToString();
                p.f_typename = "总成";
                p.f_petime = read["f_petime"].ToString();
                p.f_pstime = read["f_pstime"].ToString();
                p.f_statename = read["f_statename"].ToString();
                p.f_outtime = (int)read["f_outtime"];
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,CONVERT(varchar(100), f_pstime, 23) as f_pstime,CONVERT(varchar(100), f_petime, 23) as f_petime,f_name,f_bmwid,(case f_state when 9 then CONVERT(varchar(100), f_etime, 120) else '' end) as f_etime,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state=9) and f_bmwdid=TS_TR_BU_Work_Main_Day.f_id)*100/(select case count(*) when 0 then 1 else count(*) end  from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwdid=TS_TR_BU_Work_Main_Day.f_id) as f_percent,(select f_name from TS_S_T_S_RP_DataState where f_del=0 and f_id=TS_TR_BU_Work_Main_Day.f_state) as f_statename,(case f_state when 9 then datediff(day,f_petime,f_etime) else  datediff(day,f_petime,getdate()) end) as f_outtime from TS_TR_BU_Work_Main_Day where f_bmwid=" + _LoadBMWID + " and f_del=0 order by f_order asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TR_BU_Work_DataComplete_List();
                p.f_name = read["f_name"].ToString();
                p.f_id = "D" + read["f_id"].ToString();
                p.f_parent = "M" + read["f_bmwid"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_etime = read["f_etime"].ToString();
                p.f_typename = "流程";
                p.f_petime = read["f_petime"].ToString();
                p.f_pstime = read["f_pstime"].ToString();
                p.f_statename = read["f_statename"].ToString();
                p.f_outtime = (int)read["f_outtime"];
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,CONVERT(varchar(100), f_pstime, 23) as f_pstime,CONVERT(varchar(100), f_petime, 23) as f_petime,f_name,f_bmwid,f_bmwdid,(case f_state when 9 then CONVERT(varchar(100), f_etime, 120) else '' end) as f_etime,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state=9) and f_bmwsid=TS_TR_BU_Work_Main_Steps.f_id)*100/(select case count(*) when 0 then 1 else count(*) end  from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwsid=TS_TR_BU_Work_Main_Steps.f_id) as f_percent,(select f_name from TS_S_T_S_RP_DataState where f_del=0 and f_id=TS_TR_BU_Work_Main_Steps.f_state) as f_statename,(case f_state when 9 then datediff(day,f_petime,f_etime) else  datediff(day,f_petime,getdate()) end) as f_outtime from TS_TR_BU_Work_Main_Steps where f_bmwid=" + _LoadBMWID + " and f_del=0 order by f_order asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TR_BU_Work_DataComplete_List();
                p.f_name = read["f_name"].ToString();
                p.f_id = "S" + read["f_id"].ToString();
                p.f_parent = "D" + read["f_bmwdid"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_etime = read["f_etime"].ToString();
                p.f_typename = "工序";
                p.f_petime = read["f_petime"].ToString();
                p.f_pstime = read["f_pstime"].ToString();
                p.f_statename = read["f_statename"].ToString();
                p.f_outtime = (int)read["f_outtime"];
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,CONVERT(varchar(100), f_pstime, 23) as f_pstime,CONVERT(varchar(100), f_petime, 23) as f_petime,f_name,f_bmwid,f_bmwdid,f_bmwsid,(case f_state when 9 then CONVERT(varchar(100), f_etime, 120) else '' end) as f_etime,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state=9) and f_bmwgid=TS_TR_BU_Work_Main_Group.f_id)*100/(select case count(*) when 0 then 1 else count(*) end  from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwgid=TS_TR_BU_Work_Main_Group.f_id) as f_percent,(select f_name from TS_S_T_S_RP_DataState where f_del=0 and f_id=TS_TR_BU_Work_Main_Group.f_state) as f_statename,(case f_state when 9 then datediff(day,f_petime,f_etime) else  datediff(day,f_petime,getdate()) end) as f_outtime from TS_TR_BU_Work_Main_Group where f_bmwid=" + _LoadBMWID + " and f_del=0 order by f_order asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TR_BU_Work_DataComplete_List();
                p.f_name = read["f_name"].ToString();
                p.f_id = "G" + read["f_id"].ToString();
                p.f_parent = "S" + read["f_bmwsid"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_etime = read["f_etime"].ToString();
                p.f_typename = "工步";
                p.f_petime = read["f_petime"].ToString();
                p.f_pstime = read["f_pstime"].ToString();
                p.f_statename = read["f_statename"].ToString();
                p.f_outtime = (int)read["f_outtime"];
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_bmwid,f_bmwdid,f_bmwsid,f_bmwgid,(case f_state when 9 then 100 else 0 end) as f_percent,(select f_name from TS_S_T_S_RP_DataState where f_del=0 and f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_state) as f_statename,(case f_state when 9 then isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=4 and f_rid=TS_TR_BU_Work_Main_Group_WorkPlace.f_id) for xml path('')) , 1 , 1 , '')),' ') else '' end) as f_stuff0,(case f_state when 9 then CONVERT(varchar(100), f_time0, 120) else '' end) as f_time0,(case f_state when 9 then datediff(minute,(select f_petime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid),f_time0) else  datediff(minute,(select f_petime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid),getdate()) end) as f_outtime0,(case f_state when 9 then isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=5 and f_rid=TS_TR_BU_Work_Main_Group_WorkPlace.f_id) for xml path('')) , 1 , 1 , '')),' ') else '' end) as f_stuff1,(case f_state when 9 then CONVERT(varchar(100), f_time1, 120) else '' end) as f_time1,(case f_state when 9 then datediff(minute,(select f_petime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid),f_time1) else  datediff(minute,(select f_petime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid),getdate()) end) as f_outtime1,(case f_state when 9 then isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=6 and f_rid=TS_TR_BU_Work_Main_Group_WorkPlace.f_id) for xml path('')) , 1 , 1 , '')),' ') else '' end) as f_stuff2,(case f_state when 9 then CONVERT(varchar(100), f_time2, 120) else '' end) as f_time2  from TS_TR_BU_Work_Main_Group_WorkPlace where f_bmwid=" + _LoadBMWID + " and f_del=0 order by f_order asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TR_BU_Work_DataComplete_List();
                p.f_name = read["f_name"].ToString();
                p.f_id = "W" + read["f_id"].ToString();
                p.f_parent = "G" + read["f_bmwgid"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_typename = "项点";
                p.f_stuff0 = read["f_stuff0"].ToString();
                p.f_time0 = read["f_time0"].ToString();
                p.f_outtime0 = (int)read["f_outtime0"];
                p.f_stuff1 = read["f_stuff1"].ToString();
                p.f_time1 = read["f_time1"].ToString();
                p.f_outtime1 = (int)read["f_outtime1"];
                p.f_stuff2 = read["f_stuff2"].ToString();
                p.f_time2 = read["f_time2"].ToString();
                p.f_statename = read["f_statename"].ToString();
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Main.DataSource = pList;
            //tl_Main.Nodes.TreeList.ExpandAll();
        }
    }
}
